﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Model
{
    public class Authentication
    {
        private UTF8Encoding Encoder;
        private byte[] EncryptStringBytes;
        private MD5CryptoServiceProvider MD5Hasher;

        public Authentication()
        {
            this.Encoder = new UTF8Encoding();
            this.MD5Hasher = new MD5CryptoServiceProvider();
        }

        public string Encrypt(string s)
        {
            string str2;
            Random random = new Random();
            string str = "";
            try
            {
                while (str.Length <= 3)
                {
                    str = str + random.Next(0, 9);
                }
                this.EncryptStringBytes = this.Encoder.GetBytes(s);
                this.EncryptStringBytes = this.MD5Hasher.ComputeHash(this.EncryptStringBytes);
                str2 = BitConverter.ToString(this.EncryptStringBytes).Replace("-", null);
                string str3 = random.Next(4, str2.Length).ToString();
                str2 = str2.Insert(int.Parse(str3), str);
                if (int.Parse(str3) < 10)
                {
                    str3 = "0" + str3;
                }
                return str2.Insert(3, str3);
            }
            catch
            {
                return (str2 = "");
            }
        }

        public bool VerifyMD5Hashed(string s, string Hash)
        {
            bool flag = false;
            try
            {
                string str = Hash.Substring(3, 2);
                Hash = Hash.Remove(3, 2);
                string str2 = Hash.Substring(int.Parse(str), 4);
                Hash = Hash.Remove(int.Parse(str), 4);
                this.EncryptStringBytes = this.Encoder.GetBytes(s);
                this.EncryptStringBytes = this.MD5Hasher.ComputeHash(this.EncryptStringBytes);
                string str3 = BitConverter.ToString(this.EncryptStringBytes).Replace("-", null);
                if (str3.Equals(Hash))
                {
                    flag = true;
                }
                else if (str3 != Hash)
                {
                    flag = false;
                }
                return flag;
            }
            catch
            {
                return (flag = false);
            }
        }
    }
}
